import re


class HtmlParser(object):
    def __init__(self):
        self.zq_idx = 0
        self.xm_idx = 0
        self.dy_idx = 0

    def zq_parser(self, zq_dict, csv):
        for zq_data_dict in zq_dict:
            title = zq_data_dict['title']
            looks = zq_data_dict['online']
            name = zq_data_dict['nickname']
            typesw = zq_data_dict['gameName']
            picture_url = zq_data_dict['bpic']
            room_url = 'https://zhanqi.tv' + zq_data_dict['url']

            csv.loc[self.zq_idx] = [title, typesw, name, looks, picture_url, room_url]
            self.zq_idx += 1

    def xm_parser(self, xm_dict, csv):
        for xm_data_dict in xm_dict:
            title = xm_data_dict['name']
            typesw = xm_data_dict['classification']['cname']
            name = xm_data_dict['userinfo']['nickName']
            looks = xm_data_dict['person_num']
            picture_url = xm_data_dict['pictures']['img']
            room_url = 'http://www.panda.tv/' + xm_data_dict['id']

            csv.loc[self.xm_idx] = [title, typesw, name, looks, picture_url, room_url]
            self.xm_idx += 1

    def dy_parser(self, soup, csv):
        for lisw in soup.find_all('li'):
            if len(lisw.attrs) == 6:
                if lisw.find('span', {'class': 'dy-num fr'}):
                    title = lisw.h3.text
                    title = re.sub('(\n)|(\r)| ', '', title)
                    typesw = lisw.find('span', {'class': 'tag ellipsis'}).text
                    name = lisw.find('span', {'class': 'dy-name ellipsis fl'}).text
                    looks = lisw.find('span', {'class': 'dy-num fr'}).text
                    room_url= 'https://xiu.douyu.com' + lisw.find('a')['href']
                    picture_url = lisw.find('img')['data-original']

                    csv.loc[self.dy_idx] = [title, typesw, name, looks, picture_url, room_url]
                    # print(csv.loc[self.dy_idx])
                    self.dy_idx += 1
